﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.Entity;
using Domain.Concrete;

namespace Pro260Website.Areas.Admin.Controllers
{ 
    [Authorize(Roles="Admin")]
    public class NewsController : Controller
    {
        private EFDbContext db = EFDbContext.Instance;

        //
        // GET: /Admin/News/

        public ViewResult Index()
        {
            return View(db.NewsArticles.ToList());
        }

        //
        // GET: /Admin/News/Details/5

        public ViewResult Details(int id)
        {
            NewsArticle newsarticle = db.NewsArticles.Find(id);
            return View(newsarticle);
        }

        //
        // GET: /Admin/News/Create

        public ActionResult Create()
        {
            NewsArticle news = new NewsArticle()
            {
                PostedDate = DateTime.Now
            };
            return View(news);
        } 

        //
        // POST: /Admin/News/Create

        [HttpPost]
        public ActionResult Create(NewsArticle newsarticle)
        {
            newsarticle.PostedDate = DateTime.Now;
            if (ModelState.IsValid)
            {
                db.NewsArticles.Add(newsarticle);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(newsarticle);
        }
        
        //
        // GET: /Admin/News/Edit/5
 
        public ActionResult Edit(int id)
        {
            NewsArticle newsarticle = db.NewsArticles.Find(id);
            return View(newsarticle);
        }

        //
        // POST: /Admin/News/Edit/5

        [HttpPost]
        public ActionResult Edit(NewsArticle newsarticle)
        {
            if (ModelState.IsValid)
            {
                db.Entry(newsarticle).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(newsarticle);
        }

        //
        // GET: /Admin/News/Delete/5
 
        public ActionResult Delete(int id)
        {
            NewsArticle newsarticle = db.NewsArticles.Find(id);
            return View(newsarticle);
        }

        //
        // POST: /Admin/News/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            NewsArticle newsarticle = db.NewsArticles.Find(id);
            db.NewsArticles.Remove(newsarticle);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}